package com.smxy.mall.controller;

import com.smxy.mall.annotation.CurrentUser;
import com.smxy.mall.common.Response;
import com.smxy.mall.entity.Goods;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.GoodsReq;
import com.smxy.mall.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "商品管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @ApiOperation("添加编辑商品")
    @PostMapping("/save")
    public Object save(GoodsReq goodsReq) {
        return Response.success(goodsService.save(goodsReq));
    }

    @ApiOperation("商品列表")
    @GetMapping("/goodsList")
    public Object goodsList(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,
                            @RequestParam(value = "categoryId",required = false)Integer categoryId) {
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of((page-1),limit,sort);
        Page<Goods> result = goodsService.goodsList(categoryId,pageable);
        return Response.success(result);
    }

    @ApiOperation("商品列表联动")
    @GetMapping("/goodsListApp")
    public Object goodsListApp(@RequestParam("shopId")Integer shopId){
        return Response.success(goodsService.goodsListGroup(shopId));
    }

    @ApiOperation("推荐商品")
    @GetMapping("/recommend")
    public Object recommend() {return Response.success(goodsService.recommendList());}


    @ApiOperation("商品详情")
    @GetMapping("/goodsInfo")
    public Object goodsInfo(@RequestParam("id")Integer id) {
        if(StringUtils.isEmpty(id)){
            return Response.fail("id不能为空");
        }else {
            return Response.success(goodsService.goodsInfo(id));
        }
    }

}
